/**
 * 
 */
package com.excalibur.core.util;

import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
 * @author "Jiance Qin"
 * 
 * @date 2014年4月21日
 * 
 * @time 上午11:24:46
 * 
 * @desc
 * 
 */
public class SpringUtils {

	/**
	 * 
	 */
	public SpringUtils() {

	}

	/**
	 * 获取HttpServletRequest
	 * 
	 * @return
	 */
	public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
	}

	/**
	 * 获取HttpSession
	 * 
	 * @return
	 */
	public static HttpSession getHttpSession() {
		return getHttpServletRequest().getSession();
	}

	/**
	 * 获取ServletContext
	 * 
	 * @return
	 */
	public static ServletContext getServletContext() {
		return getHttpSession().getServletContext();
	}

	/**
	 * 获取ApplicationContext
	 * 
	 * @return
	 */
	public static ApplicationContext getApplicationContext() {
		return WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
	}

	/**
	 * 获取客户端请求的本地化信息，支持i18n处理后的结果.
	 * 
	 * @return
	 */
	public static Locale getRequestLocale() {
		return RequestContextUtils.getLocale(getHttpServletRequest());
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code, Object[] args,
			String defaultMessage, Locale locale) {
		ApplicationContext ctx = getApplicationContext();
		return ctx.getMessage(code, args, defaultMessage, locale);
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code, Object[] args,
			Locale locale) {
		ApplicationContext ctx = getApplicationContext();
		return ctx.getMessage(code, args, locale);
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code, Locale locale) {
		return getI18nMessage(code, null, locale);
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code, Object[] args,
			String defaultMessage) {
		return getI18nMessage(code, args, defaultMessage, getRequestLocale());
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code, Object[] args) {
		return getI18nMessage(code, args, getRequestLocale());
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code, String defaultMessage) {
		return getI18nMessage(code, null, defaultMessage, getRequestLocale());
	}

	/**
	 * 获取本地化后的信息.
	 * 
	 * @param code
	 * @param args
	 * @param locale
	 * @return
	 */
	public static String getI18nMessage(String code) {
		return getI18nMessage(code, null, getRequestLocale());
	}
}
